package cn.bisoft.java.util.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHAUtil
{
    public static String encrypt(byte[] obj)
    {
        MessageDigest sha = null;

        try
        {
            sha = MessageDigest.getInstance("SHA");
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }

        sha.update(obj);

        byte[] digest = sha.digest();

        StringBuffer seq = new StringBuffer(160);

        for (byte b : digest)
        {
            String s = Integer.toHexString(b);
            int len = s.length();

            if (len == 0)
            {
                seq.append("00");
            }
            else if (len == 1)
            {
                seq.append("0").append(s);
            }
            else if (len >= 2)
            {
                seq.append(s.substring(len - 2));
            }
        }

        return seq.toString();
    }

    public static void check(String str)
    {
        String seq = SHAUtil.encrypt(str.getBytes());

        // 6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
        if ("6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9".equalsIgnoreCase(seq))
        {
            System.err.println("=========== Crack 成功 =============");
            System.err.println(str + " | " + seq);
            System.err.println("=========== Crack 成功 =============");

            System.exit(-1);
        }
        else
        {
            System.out.println(str + " | " + seq.toUpperCase());
        }
    }
    
    public static void main(String[] args)
    {
        SHAUtil.check("root");
    }
    

}
